package com.ruitu.health_2.ui.common;

import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.ruitu.arad.base.base_list.BaseRefreshActivity;
import com.ruitu.arad.support.widget.progress.ProgressLayout;
import com.ruitu.health_2.R;
import com.ruitu.health_2.mvp.model.ReadPartieModel;
import com.ruitu.health_2.mvp.presenter.ReadPartiePresenter;
import com.ruitu.health_2.ui.activity.ReadPartieDetailActivity;
import com.ruitu.health_2.ui.adapter.ModelReadPartieListAdapter;
import com.ruitu.router_module.bean.PageModel2;
import com.ruitu.router_module.bean.home.ReadPartie;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

public class ReadPartieListActivity<P, M> extends BaseRefreshActivity<ReadPartiePresenter.ReadPartieListP, ReadPartieModel.ReadPartieListM> {
    private RelativeLayout rl_empty;

    private ModelReadPartieListAdapter adapter;
    private List<ReadPartie> readList = new ArrayList<>();

    protected int currPage = 1;
    protected int totalPage = 1;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_read_partie_list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rl_empty = findViewById(R.id.rl_empty);

        ((TextView) (rl_empty.findViewById(R.id.txt_description))).setText("暂时没有找到相关读书会");

        setHeadTitle("读书会列表");

        reqDataList();
        showProgress();
    }

    protected void showOrHideEmptyView() {
        if (isDataEmpty(readList)) {
            rl_empty.setVisibility(View.VISIBLE);
        } else {
            rl_empty.setVisibility(View.GONE);
        }
    }

    protected void reqDataList() {
        p.reqReadPartieList(20, currPage);
    }

    @Override
    public void onReqComplete(int code, boolean isOk, Object data) {
        if (code == 1) {
            if (currPage == 1) {
                readList.clear();
            }
            PageModel2<ReadPartie> proPage = (PageModel2<ReadPartie>) data;
            totalPage = proPage.getTotalPage();
            List<ReadPartie> newData = proPage.getList();
            readList.addAll(newData);
            adapter.setDataList(readList);
        }

        showOrHideEmptyView();
    }

    @Override
    protected void onItemClick(View v, int position) {
        Bundle bundle = new Bundle();
        bundle.putString("id", readList.get(position).getId());
        startActivity(ReadPartieDetailActivity.class, bundle);
    }

    @Override
    protected RecyclerView.Adapter initAdapter() {
        adapter = new ModelReadPartieListAdapter(this);
        return adapter;
    }

    @Override
    protected LRecyclerView findRecyclerView() {
        return findViewById(R.id.lrcv_list);
    }

    @Override
    protected void reqFirst() {
        currPage = 1;
        reqDataList();
    }

    @Override
    protected void reqMore() {
        currPage++;
        if (currPage <= totalPage) {
            reqDataList();
        } else {
            refreshNoMoreData();
        }
    }

    @Override
    protected ProgressLayout findProgressLayout() {
        return null;
    }
}
